package com.volunteer.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.common.R;
import com.volunteer.entity.Activity;
import com.volunteer.entity.Comment;
import com.volunteer.entity.Registration;
import com.volunteer.entity.User;
import com.volunteer.service.ActivityService;
import com.volunteer.service.CommentService;
import com.volunteer.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/admin/comment")
@AllArgsConstructor
public class AdminCommentController {

    private final CommentService commentService;

    private final ActivityService activityService;

    private final UserService userService;

    /**
     * 获取评价列表（分页）
     */
    @GetMapping("/list")
    public R<Page<Comment>> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.orderByDesc(Comment::getCreateTime);

        Page<Comment> page = commentService.page(
            new Page<>(pageNum, pageSize),
            queryWrapper
        );
        List<Comment> records = page.getRecords();
        if (records.isEmpty()){
            return R.ok(new Page<>());
        }
        List<Long> activityIds = records.stream().map(Comment::getActivityId).toList();
        List<Activity> activities = activityService.listByIds(activityIds);
        Map<Long, Activity> activityMap = activities.stream().collect(Collectors.toMap(Activity::getId, Function.identity()));

        List<Long> userIds = records.stream().map(Comment::getUserId).toList();
        List<User> users = userService.listByIds(userIds);
        Map<Long, User> map = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        for (Comment comment : records) {
            Activity activity = activityMap.get(comment.getActivityId());
            if (activity != null) {
                comment.setActivityName(activity.getTitle());
            }
            User user = map.get(comment.getUserId());
            if (user != null) {
                comment.setUserName(user.getName());
            }
        }
        return R.ok(page);
    }

    /**
     * 审核通过评价
     */
    @PostMapping("/approve/{id}")
    public R<String> approve(@PathVariable Long id) {
        Comment comment = new Comment();
        comment.setId(id);
        boolean success = commentService.updateById(comment);
        return success ? R.ok("审核通过成功") : R.fail("审核失败");
    }

    /**
     * 拒绝评价
     */
    @PostMapping("/reject/{id}")
    public R<String> reject(@PathVariable Long id, @RequestParam(required = false) String remark) {
        Comment comment = new Comment();
        comment.setId(id);
        boolean success = commentService.updateById(comment);
        return success ? R.ok("已拒绝该评价") : R.fail("操作失败");
    }

    /**
     * 删除评价
     */
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean success = commentService.removeById(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }

    /**
     * 根据ID获取评价详情
     */
    @GetMapping("/get/{id}")
    public R<Comment> getById(@PathVariable Long id) {
        Comment comment = commentService.getById(id);
        comment.setActivityName(activityService.getById(comment.getActivityId()).getTitle());
        return comment != null ? R.ok(comment) : R.fail("未找到该评价");
    }
}
